﻿using System;
using System.Windows;
using GiveAQuiz.Common;
using System.Reflection;
using System.ComponentModel.Composition;
using System.Net;
using System.Net.Browser;

namespace GiveAQuiz.QuizManager
{
  public partial class App : Application
  {

    int _quizDefinitionId;
    int _quizId;
    string _name;
    string _email;
    DateTime _lastVisit;

    public App()
    {
      this.Startup += this.Application_Startup;
      this.Exit += this.Application_Exit;
      this.UnhandledException += this.Application_UnhandledException;

      InitializeComponent();
    }

    /// <summary>
    /// Create an accessor to the typed app class instead of
    /// the base class' implementation.
    /// </summary>
    public static App CurrentApp
    {
      get { return App.Current as App; }
    }


    public int QuizDefinitionID
    {
      get { return _quizDefinitionId; }
      private set { _quizDefinitionId = value; }
    }

    public int QuizID
    {
      get { return _quizId; }
      private set { _quizId = value; }
    }

    public string TakerName
    {
      get { return _name; }
      private set { _name = value; }
    }

    public string TakerEMail
    {
      get { return _email; }
      private set { _email = value; }
    }

    public DateTime TakerLastVisit
    {
      get { return _lastVisit; }
      private set { _lastVisit = value; }
    }

    public bool IsNewQuiz
    {
      get;
      private set;
    }

    private void Application_Startup(object sender, StartupEventArgs e)
    {
      // Make sure the ID was passed in
      if (!e.InitParams.ContainsKey("id"))
      {
        throw new InvalidOperationException("Application must have a valid Quiz ID");
      }

      // Make sure the type was passed in
      if (!e.InitParams.ContainsKey("type"))
      {
        throw new InvalidOperationException("Application must have a valid type");
      }

      UIElement control = null;

      if (e.InitParams["type"] == "QuizTaker")
      {
        // Otherwise try and parse it  
        if (!int.TryParse(e.InitParams["id"], out _quizId))
        {
          throw new InvalidOperationException("Application must have a valid Quiz Result ID");
        }

        // Other Data Points for Takers
        TakerName = e.InitParams["name"];
        TakerEMail = e.InitParams["email"];
        DateTime.TryParse(e.InitParams["lastVisit"], out _lastVisit);

        // Create the Test Taker
        control = new QuizTaker();

      }
      else if (e.InitParams["type"] == "QuizEditor")
      {
        // Test for a new Quiz
        if (e.InitParams["id"] == "new")
        {
          IsNewQuiz = true;
        }
        // Otherwise try and parse it  
        else if (!int.TryParse(e.InitParams["id"], out _quizDefinitionId))
        {
          throw new InvalidOperationException("Application must have a valid Quiz Taker ID");
        }

        // Create the Editor
        control = new QuizEditor();
      }
      else
      {
        throw new InvalidOperationException("Application must have a valid Quiz Type");
      }

      // Show it
      this.RootVisual = control;

      // Load all the dependencies
      CompositionInitializer.SatisfyImports(control);

      // Register for showing of messages at the top level
      AppMessages.ShowMessage.Register(this, msg => MessageBox.Show(msg, "GiveAQuiz.com", MessageBoxButton.OK));
    }

    private void Application_Exit(object sender, EventArgs e)
    {
    }

    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
      // Supress User Error
      e.Handled = true;

      // TODO Create Better Error Handling
#if DEBUG
      MessageBox.Show(e.ExceptionObject.ToString(), "Debug Level Exception", MessageBoxButton.OK);
#else
      MessageBox.Show("An error occurred in the Quiz Editor. We apologize for any inconvience", "GiveAQuiz.com", MessageBoxButton.OK);
#endif
    }
  }
}
